import DataBus from '../databus'
import { SCREEN_WIDTH, SCREEN_HEIGHT } from '../render'

export default class AIMenu {
  constructor() {
    this.isVisible = false
    this.databus = new DataBus()
    
    // 按钮区域1
    this.easyButton = {
      x: SCREEN_WIDTH / 2 - 100,
      y: SCREEN_HEIGHT / 2 - 60,
      width: 200,
      height: 50
    }
    
    this.mediumButton = {
      x: SCREEN_WIDTH / 2 - 100,
      y: SCREEN_HEIGHT / 2,
      width: 200,
      height: 50
    }
    
    this.hardButton = {
      x: SCREEN_WIDTH / 2 - 100,
      y: SCREEN_HEIGHT / 2 + 60,
      width: 200,
      height: 50
    }
    
    this.backButton = {
      x: SCREEN_WIDTH / 2 - 100,
      y: SCREEN_HEIGHT / 2 + 120,
      width: 200,
      height: 50
    }
    
    // 回调函数
    this.aiGameStartCallback = null
    this.backToMainMenuCallback = null
  }

  render(ctx) {
    if (!this.isVisible) return
    
    // 绘制半透明背景
    ctx.fillStyle = 'rgba(0, 0, 0, 0.85)'
    ctx.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
    
    // 绘制装饰性背景图案
    this.drawBackgroundPattern(ctx)
    
    // 绘制标题区域
    this.drawTitle(ctx)
    
    // 绘制按钮
    this.drawButtons(ctx)
    
    // 绘制说明文字
    this.drawInstructions(ctx)
  }

  drawBackgroundPattern(ctx) {
    // 绘制渐变背景
    const gradient = ctx.createLinearGradient(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
    gradient.addColorStop(0, 'rgba(255, 152, 0, 0.3)')
    gradient.addColorStop(1, 'rgba(255, 87, 34, 0.3)')
    
    ctx.fillStyle = gradient
    ctx.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
    
    // 绘制装饰性圆点
    ctx.fillStyle = 'rgba(255, 255, 255, 0.1)'
    for (let i = 0; i < 6; i++) {
      const x = (SCREEN_WIDTH / 7) * (i + 1)
      const y = SCREEN_HEIGHT / 4 + Math.sin(Date.now() / 2000 + i) * 20
      
      ctx.beginPath()
      ctx.arc(x, y, 8, 0, 2 * Math.PI)
      ctx.fill()
    }
  }

  drawTitle(ctx) {
    // 绘制主标题
    ctx.fillStyle = '#ffffff'
    ctx.font = 'bold 36px Arial'
    ctx.textAlign = 'center'
    ctx.fillText('选择AI难度', SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4)
    
    // 绘制副标题
    ctx.font = '18px Arial'
    ctx.fillStyle = 'rgba(255, 255, 255, 0.8)'
    ctx.fillText('选择适合您的难度等级', SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4 + 30)
    
    // 绘制装饰线
    ctx.strokeStyle = 'rgba(255, 255, 255, 0.3)'
    ctx.lineWidth = 2
    ctx.beginPath()
    ctx.moveTo(SCREEN_WIDTH / 2 - 80, SCREEN_HEIGHT / 4 + 50)
    ctx.lineTo(SCREEN_WIDTH / 2 + 80, SCREEN_HEIGHT / 4 + 50)
    ctx.stroke()
  }

  drawButtons(ctx) {
    // 绘制简单难度按钮
    this.drawButton(ctx, this.easyButton, '#4CAF50', '#45a049', '简单', '#ffffff')
    
    // 绘制中等难度按钮
    this.drawButton(ctx, this.mediumButton, '#FF9800', '#F57C00', '中等', '#ffffff')
    
    // 绘制困难难度按钮
    this.drawButton(ctx, this.hardButton, '#F44336', '#D32F2F', '困难', '#ffffff')
    
    // 绘制返回按钮
    this.drawButton(ctx, this.backButton, '#9E9E9E', '#757575', '返回', '#ffffff')
  }

  drawButton(ctx, button, color, hoverColor, text, textColor) {
    // 按钮阴影
    ctx.fillStyle = 'rgba(0, 0, 0, 0.2)'
    ctx.fillRect(button.x + 2, button.y + 2, button.width, button.height)
    
    // 按钮背景渐变
    const gradient = ctx.createLinearGradient(button.x, button.y, button.x, button.y + button.height)
    gradient.addColorStop(0, color)
    gradient.addColorStop(1, hoverColor)
    ctx.fillStyle = gradient
    
    // 圆角矩形
    this.drawRoundedRect(ctx, button.x, button.y, button.width, button.height, 8)
    
    // 按钮文字
    ctx.fillStyle = textColor
    ctx.font = 'bold 18px Arial'
    ctx.textAlign = 'center'
    ctx.fillText(text, button.x + button.width / 2, button.y + button.height / 2 + 6)
  }

  drawRoundedRect(ctx, x, y, width, height, radius) {
    ctx.beginPath()
    ctx.moveTo(x + radius, y)
    ctx.lineTo(x + width - radius, y)
    ctx.quadraticCurveTo(x + width, y, x + width, y + radius)
    ctx.lineTo(x + width, y + height - radius)
    ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height)
    ctx.lineTo(x + radius, y + height)
    ctx.quadraticCurveTo(x, y + height, x, y + height - radius)
    ctx.lineTo(x, y + radius)
    ctx.quadraticCurveTo(x, y, x + radius, y)
    ctx.closePath()
    ctx.fill()
  }

  drawInstructions(ctx) {
    ctx.fillStyle = 'rgba(255, 255, 255, 0.7)'
    ctx.font = '14px Arial'
    ctx.textAlign = 'center'
    
    // 难度说明
    ctx.fillText('简单：搜索深度2层，适合初学者', SCREEN_WIDTH / 2, SCREEN_HEIGHT - 140)
    ctx.fillText('中等：搜索深度3层，使用Alpha-Beta剪枝', SCREEN_WIDTH / 2, SCREEN_HEIGHT - 120)
    ctx.fillText('困难：搜索深度4层，基础极小化极大算法', SCREEN_WIDTH / 2, SCREEN_HEIGHT - 100)

  }

  handleTouch(x, y) {
    if (!this.isVisible) return false
    
    // 检查简单难度按钮
    if (x >= this.easyButton.x && x <= this.easyButton.x + this.easyButton.width &&
        y >= this.easyButton.y && y <= this.easyButton.y + this.easyButton.height) {
      this.startAIGame('easy')
      return true
    }
    
    // 检查中等难度按钮
    if (x >= this.mediumButton.x && x <= this.mediumButton.x + this.mediumButton.width &&
        y >= this.mediumButton.y && y <= this.mediumButton.y + this.mediumButton.height) {
      this.startAIGame('medium')
      return true
    }
    
    // 检查困难难度按钮
    if (x >= this.hardButton.x && x <= this.hardButton.x + this.hardButton.width &&
        y >= this.hardButton.y && y <= this.hardButton.y + this.hardButton.height) {
      this.startAIGame('hard')
      return true
    }
    
    // 检查返回按钮
    if (x >= this.backButton.x && x <= this.backButton.x + this.backButton.width &&
        y >= this.backButton.y && y <= this.backButton.y + this.backButton.height) {
      this.backToMainMenu()
      return true
    }
    
    return false
  }

  show() {
    this.isVisible = true
  }

  hide() {
    this.isVisible = false
  }

  startAIGame(difficulty) {
    this.hide()
    // 触发AI游戏开始事件
    if (this.aiGameStartCallback) {
      this.aiGameStartCallback(difficulty)
    }
  }

  backToMainMenu() {
    this.hide()
    // 触发返回主菜单事件
    if (this.backToMainMenuCallback) {
      this.backToMainMenuCallback()
    }
  }

  // 设置AI游戏开始回调
  setAIGameStartCallback(callback) {
    this.aiGameStartCallback = callback
  }

  // 设置返回主菜单回调
  setBackToMainMenuCallback(callback) {
    this.backToMainMenuCallback = callback
  }

  update() {
    // AI菜单更新逻辑
  }
} 